/*
 * Copyright 2008-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyacinth.cloudnote.common.jpa.model;

import lombok.NoArgsConstructor;
import org.springframework.data.domain.Auditable;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.lang.Nullable;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;

/**
 * Ref: org.springframework.data.jpa.domain.AbstractAuditable
 */
@NoArgsConstructor
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditableModel extends AbstractPersistableModel implements Auditable<UUID, UUID, LocalDateTime> {

    private static final long serialVersionUID = 7291823647835658280L;

    @Nullable
    @Column(columnDefinition = "BINARY(16)")
    private UUID createdBy;

    @Nullable
    private LocalDateTime createdDate;

    @Nullable
    @Column(columnDefinition = "BINARY(16)")
    private UUID lastModifiedBy;

    @Nullable
    private LocalDateTime lastModifiedDate;

    @Version
    private long version;

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Auditable#getCreatedBy()
     */
    @Override
    @Nullable
    public Optional<UUID> getCreatedBy() {
        return Optional.ofNullable(this.createdBy);
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Auditable#setCreatedBy(java.lang.Object)
     */
    @Override
    public void setCreatedBy(final @Nullable UUID createdBy) {
        this.createdBy = createdBy;
    }

    @Override
    @Nullable
    public Optional<LocalDateTime> getCreatedDate() {
        return null == this.createdDate ? Optional.empty()
                : Optional.of(this.createdDate);
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Auditable#setCreatedDate(java.time.temporal.TemporalAccessor)
     */
    @Override
    public void setCreatedDate(final LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Auditable#getLastModifiedBy()
     */
    @Override
    @Nullable
    public Optional<UUID> getLastModifiedBy() {
        return Optional.ofNullable(this.lastModifiedBy);
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Auditable#setLastModifiedBy(java.lang.Object)
     */
    @Override
    public void setLastModifiedBy(final @Nullable UUID lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Auditable#getLastModifiedDate()
     */
    @Override
    @Nullable
    public Optional<LocalDateTime> getLastModifiedDate() {
        return null == this.lastModifiedDate ? Optional.empty()
                : Optional.of(this.lastModifiedDate);
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Auditable#setLastModifiedDate(java.time.temporal.TemporalAccessor)
     */
    @Override
    public void setLastModifiedDate(final LocalDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public long getVersion() {
        return this.version;
    }

    public void setVersion(final long version) {
        this.version = version;
    }
}
